<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con">
            <span>编辑巡检报告</span><div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="addtong">提交并发送报告</button>
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
    <div class="main-container">
        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="hidden" id="enginid">
                            <input type="text" disabled id="enginid_name" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">巡检编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>巡检名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>巡检类型</label>
                        <div class="layui-input-block">
                            <div id="xuntype"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>巡检日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">巡检部门</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=xundept&type=dept&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="xundept"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=xundept&type=dept&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="xundept_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">巡检人</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=xuner&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="xuner"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=xuner&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="xuner_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">巡检情况</label>
                        <div class="layui-input-block">
                            <textarea id="jiancon" name="jiancon" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">通知人</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=tonger&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="tonger"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=tonger&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="tonger_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>存在问题</span></div>
                        <div>
                            <button type="button" onclick="choose_engin_jian()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从检查项库导入</button>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" onclick="import_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">Excel导入</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>检查项类别</th>
                            <th><text>*</text>检查项</th>
                            <th>是否合格</th>
                            <th>说明</th>
                            <th>现场图片</th>
                            <th>是否需整改</th>
                            <th>整改措施</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>

        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;

    $("#commentForm").validate({
        rules: {
            name: {required: true},
            dt: {required: true},
        },
        messages:{
            name: {required: "请输入"},
            dt: {required: "请选择"},
        }
    });


    function choose_engin_jian() {
        openPage('../public/choose_engin_jian.php?page=engin_xun','80%','80%');
    }


    function import_list() {
        openPage('../public/import.php?reveal=engin_xun@importdata', '770px', '490px');
    }


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_engin_xun_edit, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                var engininfo = res.data.engininfo;

                if(engininfo){
                    $("#enginid").val(engininfo.id);
                    $("#enginid_name").val(engininfo.name);
                }



                $("#number").val(data.number);
                $("#name").val(data.name);
                $("#dt").val(data.dt);
                $("#jiancon").val(data.jiancon);

                xmselect_dan('xuntype', res.data.xuntype, data.xuntype, function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#xuntype").find('xm-select').removeClass('error');
                    }else{
                        $("#xuntype").find('xm-select').addClass('error');
                    }
                });

                if(data.xundept_info.length > 0){
                    tan_tags_init('#xundept', data.xundept_info);
                }

                if(data.xuner_info.length > 0){
                    tan_tags_init('#xuner', data.xuner_info);
                }

                if(data.tonger_info.length > 0){
                    tan_tags_init('#tonger', data.tonger_info);
                }


                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input jiantype"></td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td>';
                listAddStr += '<select class="layui-select ishe">';
                listAddStr += '<option value="合格" selected>合格</option>';
                listAddStr += '<option value="不合格">不合格</option>';
                listAddStr += '</select>';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" class="layui-input shuo"></td>';
                listAddStr += '<td>'+public_show_file_deta('list_list', [],1)+'</td>';
                listAddStr += '<td>';
                listAddStr += '<select class="layui-select iszheng">';
                listAddStr += '<option value="是">是</option>';
                listAddStr += '<option value="否" selected>否</option>';
                listAddStr += '</select>';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" class="layui-input gaicon"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);

                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input jiantype" value="'+list[key].jiantype+'"></td>';
                        str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                        str += '<td>';
                        str += '<select class="layui-select ishe">';
                        if(list[key].ishe == '不合格'){
                            str += '<option value="合格">合格</option>';
                            str += '<option value="不合格" selected>不合格</option>';
                        }else{
                            str += '<option value="合格" selected>合格</option>';
                            str += '<option value="不合格">不合格</option>';
                        }
                        str += '</select>';
                        str += '</td>';
                        str += '<td><input type="text" class="layui-input shuo" value="'+list[key].shuo+'"></td>';
                        str += '<td>'+public_show_file_deta('list_list', list[key].filearr)+'</td>';
                        str += '<td>';
                        str += '<select class="layui-select iszheng">';
                        if(list[key].iszheng == '是'){
                            str += '<option value="是" selected>是</option>';
                            str += '<option value="否">否</option>';
                        }else{
                            str += '<option value="是">是</option>';
                            str += '<option value="否" selected>否</option>';
                        }
                        str += '</select>';
                        str += '</td>';
                        str += '<td><input type="text" class="layui-input gaicon" value="'+list[key].gaicon+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }

                public_upload_file_load('upload_files', res.data.files);
                render_from();
                initParseMoney();
            }
        });



        form.on('submit(add)', function(data){
            parseMoney(data);


            var validRes = $("#commentForm").valid();


            if(!data.field.xuntype){
                $("#xuntype").find('xm-select').addClass('error');
                validRes = false;
            }else{
                $("#xuntype").find('xm-select').removeClass('error');
            }

            data.field.xundept = tan_tags_getvalue("#xundept", 'dept');
            data.field.xuner = tan_tags_getvalue("#xuner", 'user');
            data.field.tonger = tan_tags_getvalue("#tonger", 'user');

            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var jiantype = $(this).find('.jiantype').val();
                    var name = $(this).find('.name').val();
                    var ishe = $(this).find('.ishe').val();
                    var shuo = $(this).find('.shuo').val();
                    var files = public_deta_file_value(this);
                    var iszheng = $(this).find('.iszheng').val();
                    var gaicon = $(this).find('.gaicon').val();

                    var index = i + 1;
                    if(!name){
                        throw new Error('第'+index+'行，请输入检查项');
                    }

                    list.push({
                        jiantype:jiantype,
                        name:name,
                        ishe:ishe,
                        shuo:shuo,
                        files:files,
                        iszheng:iszheng,
                        gaicon:gaicon,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            data.field.istong = 0;


            data.field.files = public_upload_file_value('upload_files');
            data.field.id = param_id;
            data = parseMoney(data);
            facade.ajax({url: ask_engin_xun_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });
        form.on('submit(addtong)', function(data){
            parseMoney(data);


            var validRes = $("#commentForm").valid();



            if(!data.field.xuntype){
                $("#xuntype").find('xm-select').addClass('error');
                validRes = false;
            }else{
                $("#xuntype").find('xm-select').removeClass('error');
            }

            data.field.xundept = tan_tags_getvalue("#xundept", 'dept');
            data.field.xuner = tan_tags_getvalue("#xuner", 'user');
            data.field.tonger = tan_tags_getvalue("#tonger", 'user');


            if(!data.field.tonger){
                $("#tonger").parents('.tanxian').addClass('tanerror');
                $('#tonger_tanerror').text('请选择');
                validRes = false;
                facade.error("请选择通知人");
            }else{
                $("#tonger").parents('.tanxian').removeClass('tanerror');
                $('#tonger_tanerror').text('');
            }



            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var jiantype = $(this).find('.jiantype').val();
                    var name = $(this).find('.name').val();
                    var ishe = $(this).find('.ishe').val();
                    var shuo = $(this).find('.shuo').val();
                    var files = public_deta_file_value(this);
                    var iszheng = $(this).find('.iszheng').val();
                    var gaicon = $(this).find('.gaicon').val();

                    var index = i + 1;
                    if(!name){
                        throw new Error('第'+index+'行，请输入检查项');
                    }

                    list.push({
                        jiantype:jiantype,
                        name:name,
                        ishe:ishe,
                        shuo:shuo,
                        files:files,
                        iszheng:iszheng,
                        gaicon:gaicon,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            data.field.istong = 1;

            data.field.files = public_upload_file_value('upload_files');
            data.field.id = param_id;
            data = parseMoney(data);
            facade.ajax({url: ask_engin_xun_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });


    });



    var ctrlv_field_name = [
        {field: 'jiantype', name:'检查项类别'},
        {field: 'name', name:'检查项'},
        {field: 'ishe', name:'是否合格'},
        {field: 'shuo', name:'说明'},
        {field: 'iszheng', name:'是否需整改'},
        {field: 'gaicon', name:'整改措施'},
    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td><input type="text" class="layui-input jiantype" value="'+list[key].jiantype+'"></td>';
                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';

                str += ctrlvGetXiaLaStrYou(['合格','不合格'], 'ishe', list[key].ishe, 0)

                str += '<td><input type="text" class="layui-input shuo" value="'+list[key].shuo+'"></td>';
                str += '<td>'+public_show_file_deta('list_list', [],1)+'</td>';

                str += ctrlvGetXiaLaStrYou(['是','否'], 'iszheng', list[key].iszheng, 0)

                str += '<td><input type="text" class="layui-input gaicon" value="'+list[key].gaicon+'"></td>';
                str += '</tr>';
                $("#list_list").append(str);
            }
        }
        render_date();
        render_from();
    }
</script>
</body>
</html>
